# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Morten Linderud <foxboron@archlinux.org>
# Contributor: Tony Lambiris <tony@libpcap.net>

pkgname=cri-o
pkgver=1.27.0
pkgrel=1
pkgdesc="Open Container Initiative-based implementation of Kubernetes Container Runtime Interface"
arch=(loong64 x86_64)
url="https://github.com/cri-o/cri-o"
license=(Apache)
depends=(
  cni-plugins
  conmon
  conntrack-tools
  containers-common
  gcc-libs
  glibc
  gpgme # NOTE: dlopen'd
  iproute2
  iptables
  oci-runtime
)
makedepends=(
  apparmor
  btrfs-progs
  device-mapper
  go
  go-md2man
  libassuan
  libseccomp
  ostree
)
optdepends=(
  'apparmor: for apparmor integration'
  'btrfs-progs: for btrfs support'
)
provides=(container-runtime)
backup=(
  etc/crio/crio.conf
)
# configuration override and hook directories should exist
options=(emptydirs)
install=$pkgname.install
source=(
  $pkgname-$pkgver.tar.gz::https://github.com/$pkgname/$pkgname/archive/v$pkgver.tar.gz
  modules-$pkgname.conf
  sysctl-$pkgname.conf
)
sha512sums=('5219c33e86af02e3a85c8da56ee2511f5cc6d8e29cf23e27ffa213a024a3b6ddead73028235b9091dc883afca27cec5fd048a28c80f3a9ad9211ebc8f60c66f2'
            '85ee1310cb36c85c42b4068a9549a3ef72b856cd61b2c1036c3e871ef43a69ed80b43599ad94ce5b069ddd823e730596bb3d3875d4ba8cd77c4cc1985335ffff'
            'fbcc3c29d5d3dedf56ea96577dec50cb2656b4a22f5a82bf2d600ae20c6b113d35d5db116bac5520398d47a22d40835f56b86433c5713924060e1a7018d297ec')
b2sums=('704cfe3b4280ecc5b9df94dc6ae8afe5bcfda5e6816a7ddaeaaea65651ad8221488ad315f5903b536df2d6aad3ae0fb85ad6bb514ebe9a7d04e61f55c4d93262'
        '2771c64b45876d728fc139aa90754df8aadb07e14a7e1126a1488dfcbad7ebcbca923cb0230c4b87acdc6dad8243af2ad06ef4ce587d5c71fc200e40835d8b8d'
        'c8a2133df6575c54eb9bd0a89a0c6c764c0cd456bff7338c4a2e1ef4f70a5a778d5a05c335fcf2491ed55e15fc36c677853c0bc8ab66d673f1950fa2c7695f6e')

prepare() {
  cd $pkgname-$pkgver
  # make sure that /run instead of /var/run is used
  sed -e 's|/var/run|/run|g' \
      -i crictl.yaml pkg/config/config{,_unix}.go \
      docs/*.md crio-umount.conf \
      vendor/github.com/containers/conmon/runner/config/config_unix.go
  # make sure that /usr/bin is used in systemd units
  sed -e 's|/usr/local|/usr|g' -i contrib/systemd/*.service
  # set the correct default PATH for cni-plugins
  sed -e 's|/opt/cni/bin/|/usr/lib/cni/|g' -i pkg/config/config_unix.go
}

build() {
  cd $pkgname-$pkgver
  export CGO_CPPFLAGS="$CPPFLAGS"
  export CGO_CFLAGS="$CFLAGS"
  export CGO_CXXFLAGS="$CXXFLAGS"
  export CGO_LDFLAGS="$LDFLAGS"
  export GOPATH="$srcdir"
  export GOFLAGS="-buildmode=pie -mod=readonly -modcacherw"
  # NOTE: picked up by Makefile
  export EXTRA_LDFLAGS='-compressdwarf=false -linkmode external'

  make V=1 GO_MD2MAN=/usr/bin/go-md2man TRIMPATH= DEBUG=1
}

package() {
  depends+=(
    device-mapper libdevmapper.so
    libseccomp libseccomp.so
  )

  cd $pkgname-$pkgver
  # makefile is mixing DESTDIR and PREFIX
  make install install.systemd GO_MD2MAN=/usr/bin/go-md2man DESTDIR="$pkgdir" PREFIX="$pkgdir/usr"
  # drop-in config dir for CNI plugins
  install -vdm 755 "$pkgdir/etc/cni/net.d/"
  # modules
  install -vDm 644 ../modules-$pkgname.conf "$pkgdir/usr/lib/modules-load.d/cri-o.conf"
  # sysctl
  install -vDm 644 ../sysctl-$pkgname.conf "$pkgdir/usr/lib/sysctl.d/90-cri-o.conf"
  # docs
  install -vDm 644 {CONTRIBUTING,README,awesome,code-of-conduct,install,transfer,tutorial}.md -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 tutorials/*.md -t "$pkgdir/usr/share/doc/$pkgname/tutorials/"
  install -vDm 644 contrib/cni/*.{conflist,md} -t "$pkgdir/usr/share/doc/$pkgname/examples/cni/"
  install -vDm 644 contrib/metrics-exporter/{Containerfile,*.{yaml,gif,json,go}} -t "$pkgdir/usr/share/doc/$pkgname/examples/metrics-exporter/"
}
